package cn.longwingstech.intelligence.agent.controller;

import cn.longwingstech.intelligence.agent.aop.RateLimiter;
import cn.longwingstech.intelligence.agent.common.BaseResponse;
import cn.longwingstech.intelligence.agent.common.ResultUtils;
import cn.longwingstech.intelligence.agent.model.vo.message.MessageRequest;
import cn.longwingstech.intelligence.agent.mq.consumer.vo.EmailMqDTO;
import com.alibaba.fastjson2.JSON;
import jakarta.annotation.Resource;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/message")
public class MessageController {

    @Resource
    private RabbitTemplate rabbitTemplate;
    @PostMapping("/send")
    @RateLimiter(key = "genTitle", time = 60, count = 3)
    public BaseResponse<Boolean> message(@RequestBody MessageRequest messageRequest) {
        String msg = """
                用户联系：\n
                用户姓名:%s\n
                邮箱：%s\n
                电话：%s\n
                主题：%s\n
                正文：%s\n
                """;
        String content = messageRequest.getContent();
        String name = messageRequest.getName();
        String phone = messageRequest.getPhone();
        String email = messageRequest.getEmail();
        String title = messageRequest.getTitle();
        String sendContent = String.format(msg, name, email, phone, title, content);
        EmailMqDTO mqDTO = EmailMqDTO.builder()
                .title("灵犀灵溪医疗助手平台-用户联系")
                .Content(sendContent)
                .email("1058608092@qq.com").build();
        rabbitTemplate.convertAndSend("X","X-Q_EMAIL", JSON.toJSONString(mqDTO));

        return ResultUtils.success(true);
    }
}
